/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: UserServiceImpl.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年4月25日 下午1:51:35
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.user.service;

import java.util.Date;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.cpt.framework.core.OperationResult;
import com.material.c2c.config.UserStatus;
import com.material.c2c.core.UUIDGenerator;
import com.material.c2c.user.dao.UserDao;
import com.material.c2c.user.entity.USER;

/**
 * 
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
@Service
public class UserServiceImpl implements UserService{
	@Resource
	UserDao userDao;
	
	@Transactional
	@Override
	public OperationResult add(USER user) {
		user.setId(UUIDGenerator.generateUUIDStr());
		user.setDjqk(UserStatus.NO);
		user.setZcsj(new Date());
		user.setDlcs(0);
		user = userDao.add(user);
		if(user==null) {
			return OperationResult.failed("添加失败！");
		}
		return OperationResult.SUCCEED;
	}
	@Override
	public Page<USER> search(String key,Pageable page) {
		
		return userDao.findAll(page);
	}
	@Transactional
	@Override
	public OperationResult update(USER user) {
		user = userDao.update(user);
		if(user==null) {
			return OperationResult.failed("更新失败！");
		}
		return OperationResult.SUCCEED;
	}
	@Transactional
	@Override
	public OperationResult delete(String id) {
		userDao.delete(id);
		return OperationResult.SUCCEED;
	}
	@Override
	public USER getOne(String id) {
		return userDao.getOne(id);
	}
	@Override
	public USER findUser(String key) {
	  return userDao.findUser(key);
	}
	@Override
	@Transactional
	public OperationResult loginTiemPlus(USER user) {
		int times = user.getDlcs();
		times++;
		user.setDlcs(times);
		userDao.update(user);
		return OperationResult.SUCCEED;
	}
}
